class SalasdeaulasController < ApplicationController
  # GET /salasdeaulas
  # GET /salasdeaulas.json
  def index
    @predios = Predio.find(:all, :order => "nome ASC")

    @salas = Salasdeaula.find(:all, :conditions => ["predio_id = ?", params[:predio_id]], :order => "numero")
    if !params[:predio_id].blank?
      @predio = Predio.find(:first, :conditions => ["id = ?", params[:predio_id]])
    end


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @salasdeaulas }
    end
  end

  # GET /salasdeaulas/1
  # GET /salasdeaulas/1.json
  def show
    @salasdeaula = Salasdeaula.find(params[:id])

    @predio = Predio.find(:first, :conditions => ["id = ?", @salasdeaula.predio_id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @salasdeaula }
    end
  end

  # GET /salasdeaulas/new
  # GET /salasdeaulas/new.json
  def new
    @predios = Predio.all(:order => "nome ASC").collect {|s| [ s.nome, s.id ] }
    @sala = Salasdeaula.new
    

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @salasdeaula }
    end
  end

  # GET /salasdeaulas/1/edit
  def edit
    @sala = Salasdeaula.find(params[:id])
    @predio = Predio.find(:first, :conditions => ["id = ?", @sala.predio_id])
  end

  # POST /salasdeaulas
  # POST /salasdeaulas.json
  def create
    @salasdeaula = Salasdeaula.new(params[:salasdeaula])

    respond_to do |format|
      if @salasdeaula.save
        format.html { redirect_to @salasdeaula, notice: 'Sala de aula criado com sucesso.' }
        format.json { render json: @salasdeaula, status: :created, location: @salasdeaula }
      else
        format.html { render action: "new" }
        format.json { render json: @salasdeaula.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /salasdeaulas/1
  # PUT /salasdeaulas/1.json
  def update
    @sala = Salasdeaula.find(params[:id])
    @predio = Predio.find(:first, :conditions => ["id = ?", @sala.predio_id])

    respond_to do |format|
      if @sala.update_attributes(params[:salasdeaula])
        format.html { redirect_to @sala, notice: 'Sala de aula atualizado com sucesso.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @sala.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /salasdeaulas/1
  # DELETE /salasdeaulas/1.json
  def destroy
    @salasdeaula = Salasdeaula.find(params[:id])
    @salasdeaula.destroy

    respond_to do |format|
      format.html { redirect_to salasdeaulas_url }
      format.json { head :no_content }
    end
  end
end
